package com.zut.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;

/**
 * @author: Mr.Liu
 * @create: 2021-11-16 22:09
 */
@Component
public class MailUtil {
    @Autowired
    JavaMailSenderImpl mailSender;

    @Async//异步任务
    public void sendMail(String email ,String username ,String password) throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //组装
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

        //主题
        helper.setSubject("QR二维码");
        //加载图片
        MimeBodyPart image = new MimeBodyPart();
        DataHandler dh = new DataHandler(new FileDataSource("D:\\QR\\qr-code-back\\src\\main\\resources\\static\\1.jpg"));
        image.setDataHandler(dh);
        image.setContentID("bz.jpg");
//        helper.addAttachment("1.jpg",new File("D:\\QR\\qr-code-back\\src\\main\\resources\\static\\1.jpg"));
        //发送人
        helper.setFrom("464685873@qq.com");
        //接收人
        helper.setTo(email);
        //内容
        String info = "恭喜您注册成功！</br>您的用户名是："+username+",您的密码："+password+"</br><p style='color:red'>请妥善保管，如有问题请联系管理员！！</p>";
        helper.setText(info,true);
        mailSender.send(mimeMessage);
    }


}
